package org.jeecg.modules.quartz.job;

import java.math.BigDecimal;
import java.util.List;

import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.finer.erp.jeecg.bas.entity.BasMaterial;
import io.finer.erp.jeecg.bas.entity.BasSupplier;
import io.finer.erp.jeecg.bas.entity.Lingliaodan;
import io.finer.erp.jeecg.bas.entity.Rukudan;
import io.finer.erp.jeecg.bas.entity.Weiwaijiagong;
import io.finer.erp.jeecg.bas.entity.Xiaoshouchuku;
import io.finer.erp.jeecg.bas.service.IBasMaterialService;
import io.finer.erp.jeecg.bas.service.IBasSupplierService;
import io.finer.erp.jeecg.bas.service.ILingliaodanService;
import io.finer.erp.jeecg.bas.service.IRukudanService;
import io.finer.erp.jeecg.bas.service.IWeiwaijiagongService;
import io.finer.erp.jeecg.bas.service.IXiaoshouchukuService;
import io.finer.erp.jeecg.stock.entity.ProcessingRecord;
import io.finer.erp.jeecg.stock.entity.StkInventory;
import io.finer.erp.jeecg.stock.entity.StkIoBill;
import io.finer.erp.jeecg.stock.entity.StkIoBillEntry;
import io.finer.erp.jeecg.stock.service.IProcessingRecordService;
import io.finer.erp.jeecg.stock.service.IStkInventoryService;
import io.finer.erp.jeecg.stock.service.IStkIoBillEntryService;
import io.finer.erp.jeecg.stock.service.IStkIoBillService;

/**
 * 
 * @ClassName: KindeeGenerateJob 
 * @Description: 金蝶凭证对接--定时任务（采购单、销售收入、制胶、复合、分切、切张、辅料、销售成本、代加工） 
 * @author heckerstone heckerstone@163.com
 * @date Oct 29, 2020 9:08:00 AM 
 *
 */
public class KindeeGenerateJob implements Job {
	@Autowired
	private IStkIoBillService stkIoBillService;
	@Autowired
	private IStkIoBillEntryService stkIoBillEntryService;
	//	@Autowired
	//	private IZrkKindeeProofCgService zrkKindeeProofCgService;//采购
	//	@Autowired
	//	private IZrkKindeeProofDjgService zrkKindeeProofDjgService;//代加工
	//	@Autowired
	//	private IZrkKindeeProofFhService zrkKindeeProofFhService;//复合
	//	@Autowired
	//	private IZrkKindeeProofFlService zrkKindeeProofFlService;//辅料
	//	@Autowired
	//	private IZrkKindeeProofFqService zrkKindeeProofFqService;//分切
	//	@Autowired
	//	private IZrkKindeeProofQzService zrkKindeeProofQzService;//切张
	//	@Autowired
	//	private IZrkKindeeProofSkService zrkKindeeProofSkService;//收款
	//	@Autowired
	//	private IZrkKindeeProofThService zrkKindeeProofThService;//退货
	//	@Autowired
	//	private IZrkKindeeProofXsckService zrkKindeeProofXsckService;//销售出库
	//	@Autowired
	//	private IZrkKindeeProofZjService zrkKindeeProofZjService;//制胶
	@Autowired
	private IBasSupplierService basSupplierService;
//	@Autowired
//	private IFinPayableService finPayableService;
//	@Autowired
//	private IFinPaymentService finPaymentService;
//	@Autowired
//	private IBasBankAccountService basBankAccountService;
	@Autowired
	private IProcessingRecordService processingRecordServie;
	@Autowired
	private IStkInventoryService stkInventoryService;
//	@Autowired
//	private IBasCustomerService basCustomerService;
//	@Autowired
//	private IFinReceivableService finReceivableService;
//	@Autowired
//	private IFinReceiptService finReceiptService;
	@Autowired
	private ILingliaodanService lingliaodanService;
	@Autowired
	private IRukudanService rukudanService;
	@Autowired
	private ISysDepartService sysDepartService;
	@Autowired
	private IWeiwaijiagongService weiwaijiagongService;
	@Autowired
	private IBasMaterialService basMaterialService;
	@Autowired
	private IXiaoshouchukuService xiaoshouchukuService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		//该定时任务每月启动一次，启动后检测当月所有在当月产生的单据然后向中间表中插入单据凭证(总和记录一条或者有多少条记录多少条)
		//获取当前月第一天
		String firstDay=DateUtils.getFirstDayOfMonth(DateUtils.getMonth());
		//获取最后一天
		String lastDay=DateUtils.getLastDayOfMonth(DateUtils.getMonth());
		//生成采购单凭证
		generateCG(firstDay, lastDay);
		//生成代加工凭证
		generateDJG(firstDay,lastDay);
		//复合凭证生成
		generateFH(firstDay,lastDay);
		//分切
		generateFQ(firstDay,lastDay);
		//切张
		generateQZ(firstDay,lastDay);
		//辅料
		generateFL(firstDay,lastDay);
		//销售凭证
		generateXS(firstDay,lastDay);
	}

	/**
	 * 
	 * @Title: generateQZ 
	 * @Description: 辅料凭证生成 
	 * @author heckerstone heckerstone@163.com
	 * @date Oct 29, 2020 2:34:49 PM 
	 * @param @param firstDay
	 * @param @param lastDay    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void generateFL(String firstDay,String lastDay) {
		//根据上述时间段查询所有切张单
		QueryWrapper<StkIoBill> queryWrapper=new QueryWrapper<StkIoBill>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper.and(wrapper->wrapper.eq("stock_io_type", "1996"));//查询辅料入库单
		queryWrapper.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList=stkIoBillService.list(queryWrapper);
		if(null!=stkIoBillList&&stkIoBillList.size()>0) {
			for(int i=0;i<stkIoBillList.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList.get(i);
				//未生成凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					//金蝶凭证--借
					Rukudan zkpf=new Rukudan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("切张--产品入库");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Rukudan zkpf2=new Rukudan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("切张--产品入库");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—切张车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					rukudanService.save(zkpf);
					rukudanService.save(zkpf2);
					//将该条数据修改为已生成凭证
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		/***************************辅料出库*************************************/
		//根据上述时间段查询所有采购单
		QueryWrapper<StkIoBill> queryWrapper3=new QueryWrapper<StkIoBill>();
		queryWrapper3.ge("create_time", firstDay);
		queryWrapper3.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper3.and(wrapper->wrapper.eq("stock_io_type", "2996"));//查询辅料出库单
		queryWrapper3.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper3.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper3.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList2=stkIoBillService.list(queryWrapper3);
		if(null!=stkIoBillList2&&stkIoBillList2.size()>0) {
			for(int i=0;i<stkIoBillList2.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList2.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Lingliaodan zkpf=new Lingliaodan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("切张--生产领料");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Lingliaodan zkpf2=new Lingliaodan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("切张--生产领料");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—切张车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					lingliaodanService.save(zkpf);
					lingliaodanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
	}

	/**
	 * 
	 * @Title: generateQZ 
	 * @Description: 切张凭证生成 
	 * @author heckerstone heckerstone@163.com
	 * @date Oct 29, 2020 2:34:49 PM 
	 * @param @param firstDay
	 * @param @param lastDay    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void generateQZ(String firstDay,String lastDay) {
		//根据上述时间段查询所有切张单
		QueryWrapper<StkIoBill> queryWrapper=new QueryWrapper<StkIoBill>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper.and(wrapper->wrapper.eq("stock_io_type", "1993"));//查询分切入库单
		queryWrapper.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList=stkIoBillService.list(queryWrapper);
		if(null!=stkIoBillList&&stkIoBillList.size()>0) {
			for(int i=0;i<stkIoBillList.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Rukudan zkpf=new Rukudan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("切张--产品入库");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Rukudan zkpf2=new Rukudan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("切张--产品入库");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—切张车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					rukudanService.save(zkpf);
					rukudanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		/***************************切张出库*************************************/
		//根据上述时间段查询所有采购单
		QueryWrapper<StkIoBill> queryWrapper3=new QueryWrapper<StkIoBill>();
		queryWrapper3.ge("create_time", firstDay);
		queryWrapper3.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper3.and(wrapper->wrapper.eq("stock_io_type", "2992"));//查询复合入库单
		queryWrapper3.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper3.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper3.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList2=stkIoBillService.list(queryWrapper3);
		if(null!=stkIoBillList2&&stkIoBillList2.size()>0) {
			for(int i=0;i<stkIoBillList2.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList2.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Lingliaodan zkpf=new Lingliaodan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("切张--生产领料");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Lingliaodan zkpf2=new Lingliaodan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("切张--生产领料");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—切张车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					lingliaodanService.save(zkpf);
					lingliaodanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
	}

	private void generateFQ(String firstDay,String lastDay) {
		//根据上述时间段查询所有分切单
		QueryWrapper<StkIoBill> queryWrapper=new QueryWrapper<StkIoBill>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper.and(wrapper->wrapper.eq("stock_io_type", "1993"));//查询分切入库单
		queryWrapper.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList=stkIoBillService.list(queryWrapper);
		if(null!=stkIoBillList&&stkIoBillList.size()>0) {
			for(int i=0;i<stkIoBillList.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Rukudan zkpf=new Rukudan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("分切--产品入库");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Rukudan zkpf2=new Rukudan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("分切--产品入库");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—复合车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					rukudanService.save(zkpf);
					rukudanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		/***************************分切出库*************************************/
		//根据上述时间段查询所有采购单
		QueryWrapper<StkIoBill> queryWrapper3=new QueryWrapper<StkIoBill>();
		queryWrapper3.ge("create_time", firstDay);
		queryWrapper3.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper3.and(wrapper->wrapper.eq("stock_io_type", "2992"));//查询复合入库单
		queryWrapper3.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper3.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper3.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList2=stkIoBillService.list(queryWrapper3);
		if(null!=stkIoBillList2&&stkIoBillList2.size()>0) {
			for(int i=0;i<stkIoBillList2.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList2.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Lingliaodan zkpf=new Lingliaodan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("分切--生产领料");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Lingliaodan zkpf2=new Lingliaodan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("分切--生产领料");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—分切车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					lingliaodanService.save(zkpf);
					lingliaodanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
	}

	private void generateFH(String firstDay,String lastDay) {
		//根据上述时间段查询所有采购单
		QueryWrapper<StkIoBill> queryWrapper=new QueryWrapper<StkIoBill>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper.and(wrapper->wrapper.eq("stock_io_type", "1992"));//查询复合入库单
		queryWrapper.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList=stkIoBillService.list(queryWrapper);
		if(null!=stkIoBillList&&stkIoBillList.size()>0) {
			for(int i=0;i<stkIoBillList.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Rukudan zkpf=new Rukudan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("复合--产品入库");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("半成品");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Rukudan zkpf2=new Rukudan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("复合--产品入库");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("生产成本—复合车间");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					rukudanService.save(zkpf);
					rukudanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		/***************************复合出库*************************************/
		//根据上述时间段查询所有采购单
		QueryWrapper<StkIoBill> queryWrapper3=new QueryWrapper<StkIoBill>();
		queryWrapper3.ge("create_time", firstDay);
		queryWrapper3.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper3.and(wrapper->wrapper.eq("stock_io_type", "2991"));//查询复合入库单
		queryWrapper3.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper3.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper3.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList2=stkIoBillService.list(queryWrapper3);
		if(null!=stkIoBillList2&&stkIoBillList2.size()>0) {
			for(int i=0;i<stkIoBillList2.size();i++) {
				BigDecimal total=BigDecimal.ZERO;
				StkIoBill sib=stkIoBillList2.get(i);
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt()==null?BigDecimal.ZERO:si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//金蝶凭证--借
					Lingliaodan zkpf=new Lingliaodan();
					zkpf.setCreateBy("admin");
					zkpf.setCreateTime(sib.getCreateTime());
					zkpf.setBillNo(sib.getBillNo());
					zkpf.setBillType("复合--生产领料");
					zkpf.setBillMoney(total);
					zkpf.setAccountingType("生产成本—复合车间");
					zkpf.setKindeeType("借");
					zkpf.setGroupId((i+1)+"");
					zkpf.setDepartId(departId);
					zkpf.setDepartName(departName);
					//金蝶凭证--贷
					Lingliaodan zkpf2=new Lingliaodan();
					zkpf2.setCreateBy("admin");
					zkpf2.setCreateTime(sib.getCreateTime());
					zkpf2.setBillNo(sib.getBillNo());
					zkpf2.setBillType("复合--生产领料");
					zkpf2.setBillMoney(total);
					zkpf2.setAccountingType("原材料/库存商品（单指胶制品）");
					zkpf2.setKindeeType("贷");
					zkpf2.setGroupId((i+1)+"");
					zkpf2.setDepartId(departId);
					zkpf2.setDepartName(departName);
					lingliaodanService.save(zkpf);
					lingliaodanService.save(zkpf2);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
	}

	/**
	 * 
	 * @Title: generateDJG 
	 * @Description: 代加工中间表数据生成
	 * @author heckerstone heckerstone@163.com
	 * @date Oct 29, 2020 2:15:07 PM 
	 * @param @param firstDay
	 * @param @param lastDay    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void generateDJG(String firstDay,String lastDay) {
		//根据上述时间段查询所有采购单
		QueryWrapper<ProcessingRecord> queryWrapper=new QueryWrapper<ProcessingRecord>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		List<ProcessingRecord> prList=processingRecordServie.list(queryWrapper);
		if(null!=prList&&prList.size()>0) {
			for(int i=0;i<prList.size();i++) {
				ProcessingRecord pr=prList.get(i);
				//查询旧物料--材料费
				QueryWrapper<StkInventory> siQw=new QueryWrapper<StkInventory>();
				siQw.eq("warehouse_id","1315841495892500482");//代加工库编码
				StkInventory si=stkInventoryService.getOne(siQw.last("LIMIT 1"));
				BigDecimal clFee=BigDecimal.ZERO;
				if(null!=si) {
					clFee=si.getCost();
				}
				String supplierId="";
				String supplierName="";
				BasMaterial bm=basMaterialService.getById(pr.getRawMaterialId());
				//查询原物料供应商
				if(null!=bm) {
					BasSupplier bs=basSupplierService.getById(bm.getSupplier());
					if(null!=bs) {
						supplierId=bs.getId();
						supplierName=bs.getName();
					}
				}
				//构造中间表数据--借
				Weiwaijiagong zkcg=new Weiwaijiagong();
				zkcg.setCreateBy("admin");
				zkcg.setCreateTime(pr.getCreateTime());
				zkcg.setBillNo(pr.getId());
				zkcg.setBillType("代加工");
				zkcg.setBillMoney(clFee.add(pr.getAmount()));
				zkcg.setAccountingType("库存商品或者原材料或者半成品");
				zkcg.setKindeeType("借");
				zkcg.setGroupId((i+1)+"");
				zkcg.setSupplierId(supplierId);
				zkcg.setSupplierName(supplierName);
				//构造中间表数据--贷
				Weiwaijiagong zkcgD=new Weiwaijiagong();
				zkcgD.setCreateBy("admin");
				zkcgD.setCreateTime(pr.getCreateTime());
				zkcgD.setBillNo(pr.getId());
				zkcgD.setBillType("代加工");
				zkcgD.setBillMoney(clFee);
				zkcgD.setAccountingType("库存商品或者原材料或者半成品");
				zkcgD.setKindeeType("贷");
				zkcgD.setGroupId((i+1)+"");
				zkcgD.setSupplierId(supplierId);
				zkcgD.setSupplierName(supplierName);
				//构造中间表数据--贷
				Weiwaijiagong zkcg3=new Weiwaijiagong();
				zkcg3.setCreateBy("admin");
				zkcg3.setCreateTime(pr.getCreateTime());
				zkcg3.setBillNo(pr.getId());
				zkcg3.setBillType("代加工");
				zkcg3.setBillMoney(pr.getAmount());
				zkcg3.setAccountingType("应付账款");
				zkcg3.setKindeeType("贷");
				zkcg3.setGroupId((i+1)+"");
				zkcg3.setSupplierId(supplierId);
				zkcg3.setSupplierName(supplierName);
				//保存数据
				weiwaijiagongService.save(zkcg);
				weiwaijiagongService.save(zkcgD);
				weiwaijiagongService.save(zkcg3);
			}
		}
	}

	/**
	 * 
	 * @Title: generateCG 
	 * @Description: 生成采购单方法抽离 
	 * @author heckerstone heckerstone@163.com
	 * @date Oct 29, 2020 10:04:53 AM 
	 * @param @param firstDay
	 * @param @param lastDay    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void generateCG(String firstDay, String lastDay) {
		//根据上述时间段查询所有采购单
		QueryWrapper<StkIoBill> queryWrapper=new QueryWrapper<StkIoBill>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper.and(wrapper->wrapper.eq("stock_io_type", "101"));//查询采购入库单
		queryWrapper.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList=stkIoBillService.list(queryWrapper);
		//非空验证--采购入库单
		if(null!=stkIoBillList&&stkIoBillList.size()>0) {
			for(int i=0;i<stkIoBillList.size();i++) {
				StkIoBill sib=stkIoBillList.get(i);
				BigDecimal total=BigDecimal.ZERO;
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//构造中间表数据--借
					Rukudan zkcg=new Rukudan();
					zkcg.setCreateBy("admin");
					zkcg.setCreateTime(sib.getCreateTime());
					zkcg.setBillNo(sib.getBillNo());
					zkcg.setBillType("采购单入库");
					zkcg.setBillMoney(total);
					zkcg.setAccountingType("原材料");
					zkcg.setKindeeType("借");
					zkcg.setGroupId((i+1)+"");
					zkcg.setDepartId(departId);
					zkcg.setDepartName(departName);
					//构造中间表数据--贷
					Rukudan zkcgD=new Rukudan();
					zkcgD.setCreateBy("admin");
					zkcgD.setCreateTime(sib.getCreateTime());
					zkcgD.setBillNo(sib.getBillNo());
					zkcgD.setBillType("采购单入库");
					zkcgD.setBillMoney(total);
					zkcgD.setAccountingType("应付账款");
					zkcgD.setKindeeType("贷");
					zkcgD.setGroupId((i+1)+"");
					zkcgD.setDepartId(departId);
					zkcgD.setDepartName(departName);
					//保存数据
					rukudanService.save(zkcg);
					rukudanService.save(zkcgD);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		//采购退货出库
		QueryWrapper<StkIoBill> thckQw=new QueryWrapper<StkIoBill>();
		thckQw.ge("create_time", firstDay);
		thckQw.and(wrapper->wrapper.le("create_time", lastDay));
		thckQw.and(wrapper->wrapper.eq("stock_io_type", "101"));//查询采购入库单
		thckQw.and(wrapper->wrapper.eq("is_rubric", "1"));//采购退货标志
		thckQw.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		thckQw.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		thckQw.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillTHCKList=stkIoBillService.list(thckQw);
		if(null!=stkIoBillTHCKList&&stkIoBillTHCKList.size()>0) {
			//构造采购退货凭证
			for(int i=0;i<stkIoBillList.size();i++) {
				StkIoBill sib=stkIoBillList.get(i);
				BigDecimal total=BigDecimal.ZERO;
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//构造中间表数据--借
					Rukudan zkcg=new Rukudan();
					zkcg.setCreateBy("admin");
					zkcg.setCreateTime(DateUtils.getDate());
					zkcg.setBillNo(sib.getBillNo());
					zkcg.setBillType("采购退货");
					zkcg.setBillMoney(total);
					zkcg.setAccountingType("应付账款");
					zkcg.setKindeeType("借");
					zkcg.setGroupId((i+1)+"");
					zkcg.setDepartId(departId);
					zkcg.setDepartName(departName);
					//构造中间表数据--贷
					Rukudan zkcgD=new Rukudan();
					zkcgD.setCreateBy("admin");
					zkcgD.setCreateTime(DateUtils.getDate());
					zkcgD.setBillNo(sib.getBillNo());
					zkcgD.setBillType("采购退货");
					zkcgD.setBillMoney(total);
					zkcgD.setAccountingType("原材料");
					zkcgD.setKindeeType("贷");
					zkcgD.setGroupId((i+1)+"");
					zkcgD.setDepartId(departId);
					zkcgD.setDepartName(departName);
					//保存数据
					rukudanService.save(zkcg);
					rukudanService.save(zkcgD);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		//		//采购应付单--凭证
		//		QueryWrapper<FinPayable> fpQw=new QueryWrapper<FinPayable>();
		//		//		fpQw.eq("source_no", sib.getBillNo());
		//		fpQw.ge("create_time", firstDay);
		//		fpQw.and(wrapper->wrapper.le("create_time", lastDay));
		//		fpQw.and(wrapper->wrapper.eq("is_approved", "1"));//审核通过
		//		fpQw.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		//		fpQw.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		//		fpQw.and(wrapper->wrapper.eq("payable_type", "202"));//采购应付
		//		List<FinPayable> fpableList=finPayableService.list(fpQw);
		//		//非空
		//		if(null!=fpableList&&fpableList.size()>0) {
		//			for(int j=0;j<fpableList.size();j++) {
		//				FinPayable fp=fpableList.get(j);
		//				//查询供应商信息
		//				BasSupplier bs=basSupplierService.getById(fp.getSupplierId());
		//				String sId="";
		//				String sName="";
		//				try {
		//					sId=bs.getId();
		//					sName=bs.getName();
		//				} catch (Exception e) {
		//				}
		//				//构造中间表数据--借
		//				ZrkKindeeProofCg zkcg2=new ZrkKindeeProofCg();
		//				zkcg2.setCreateBy("admin");
		//				zkcg2.setCreateTime(fp.getCreateTime());
		//				zkcg2.setBillNo(fp.getBillNo());
		//				zkcg2.setBillType("采购应付单");
		//				zkcg2.setBillMoney(fp.getAmt());
		//				zkcg2.setAccountingType("应付账款");
		//				zkcg2.setKindeeType("借");
		//				zkcg2.setGroupId((j+1)+"");
		//				zkcg2.setSupplierId(sId);
		//				zkcg2.setSupplierName(sName);
		//				//构造中间表数据--贷
		//				ZrkKindeeProofCg zkcg3=new ZrkKindeeProofCg();
		//				zkcg3.setCreateBy("admin");
		//				zkcg3.setCreateTime(fp.getCreateTime());
		//				zkcg3.setBillNo(fp.getBillNo());
		//				zkcg3.setBillType("采购应付单");
		//				zkcg3.setBillMoney(fp.getAmt());
		//				zkcg3.setAccountingType("银行存款-"+bankname+bankCardNo);
		//				zkcg3.setKindeeType("贷");
		//				zkcg3.setGroupId((j+1)+"");
		//				zkcg3.setSupplierId(sId);
		//				zkcg3.setSupplierName(sName);
		//				//保存数据
		//				zrkKindeeProofCgService.save(zkcg2);
		//				zrkKindeeProofCgService.save(zkcg3);
		//			}
		//		}
		//		//采购预付单--凭证
		//		QueryWrapper<FinPayment> fpQw2=new QueryWrapper<FinPayment>();
		//		//				fpQw.eq("source_no", sib.getBillNo());
		//		fpQw2.ge("create_time", firstDay);
		//		fpQw2.and(wrapper->wrapper.le("create_time", lastDay));
		//		fpQw2.and(wrapper->wrapper.eq("is_approved", "1"));
		//		fpQw2.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		//		fpQw2.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		//		fpQw2.and(wrapper->wrapper.eq("payment_type", "201"));//采购预付类型
		//		List<FinPayment> fpmentList=finPaymentService.list(fpQw2);
		//		//非空
		//		if(null!=fpmentList&&fpmentList.size()>0) {
		//			for(int j=0;j<fpmentList.size();j++) {
		//				FinPayment fp=fpmentList.get(j);
		//				//查询供应商信息
		//				BasSupplier bs=basSupplierService.getById(fp.getSupplierId());
		//				String sId="";
		//				String sName="";
		//				try {
		//					sId=bs.getId();
		//					sName=bs.getName();
		//				} catch (Exception e) {
		//				}
		//				//构造中间表数据--借
		//				ZrkKindeeProofCg zkcg2=new ZrkKindeeProofCg();
		//				zkcg2.setCreateBy("admin");
		//				zkcg2.setCreateTime(fp.getCreateTime());
		//				zkcg2.setBillNo(fp.getBillNo());
		//				zkcg2.setBillType("采购预付单");
		//				zkcg2.setBillMoney(fp.getAmt());
		//				zkcg2.setAccountingType("应付账款");
		//				zkcg2.setKindeeType("借");
		//				zkcg2.setGroupId((j+1)+"");
		//				zkcg2.setSupplierId(bs.getId());
		//				zkcg2.setSupplierName(bs.getName());
		//				//构造中间表数据--贷
		//				ZrkKindeeProofCg zkcg3=new ZrkKindeeProofCg();
		//				zkcg3.setCreateBy("admin");
		//				zkcg3.setCreateTime(fp.getCreateTime());
		//				zkcg3.setBillNo(fp.getBillNo());
		//				zkcg3.setBillType("采购预付单");
		//				zkcg3.setBillMoney(fp.getAmt());
		//				zkcg3.setAccountingType("银行存款-"+bankname+bankCardNo);
		//				zkcg3.setKindeeType("贷");
		//				zkcg3.setGroupId((j+1)+"");
		//				zkcg3.setSupplierId(sId);
		//				zkcg3.setSupplierName(sName);
		//				//保存数据
		//				zrkKindeeProofCgService.save(zkcg2);
		//				zrkKindeeProofCgService.save(zkcg3);
		//			}
		//		}
		//		//采购付款单--凭证
		//		QueryWrapper<FinPayment> fpQw3=new QueryWrapper<FinPayment>();
		//		//				fpQw.eq("source_no", sib.getBillNo());
		//		fpQw3.ge("create_time", firstDay);
		//		fpQw3.and(wrapper->wrapper.le("create_time", lastDay));
		//		fpQw3.and(wrapper->wrapper.eq("is_approved", "1"));
		//		fpQw3.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		//		fpQw3.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		//		fpQw3.and(wrapper->wrapper.eq("payment_type", "202"));//采购付款类型
		//		List<FinPayment> fpment2List=finPaymentService.list(fpQw3);
		//		//非空
		//		if(null!=fpment2List&&fpment2List.size()>0) {
		//			for(int j=0;j<fpment2List.size();j++) {
		//				FinPayment fp=fpment2List.get(j);
		//				//查询供应商信息
		//				BasSupplier bs=basSupplierService.getById(fp.getSupplierId());
		//				String sId="";
		//				String sName="";
		//				try {
		//					sId=bs.getId();
		//					sName=bs.getName();
		//				} catch (Exception e) {
		//				}
		//				//构造中间表数据--借
		//				ZrkKindeeProofCg zkcg2=new ZrkKindeeProofCg();
		//				zkcg2.setCreateBy("admin");
		//				zkcg2.setCreateTime(fp.getCreateTime());
		//				zkcg2.setBillNo(fp.getBillNo());
		//				zkcg2.setBillType("采购付款单");
		//				zkcg2.setBillMoney(fp.getAmt());
		//				zkcg2.setAccountingType("应付账款");
		//				zkcg2.setKindeeType("借");
		//				zkcg2.setGroupId((j+1)+"");
		//				zkcg2.setSupplierId(sId);
		//				zkcg2.setSupplierName(sName);
		//				//构造中间表数据--贷
		//				ZrkKindeeProofCg zkcg3=new ZrkKindeeProofCg();
		//				zkcg3.setCreateBy("admin");
		//				zkcg3.setCreateTime(fp.getCreateTime());
		//				zkcg3.setBillNo(fp.getBillNo());
		//				zkcg3.setBillType("采购付款单");
		//				zkcg3.setBillMoney(fp.getAmt());
		//				zkcg3.setAccountingType("银行存款-"+bankname+bankCardNo);
		//				zkcg3.setKindeeType("贷");
		//				zkcg3.setGroupId((j+1)+"");
		//				zkcg3.setSupplierId(sId);
		//				zkcg3.setSupplierName(sName);
		//				//保存数据
		//				zrkKindeeProofCgService.save(zkcg2);
		//				zrkKindeeProofCgService.save(zkcg3);
		//			}
		//		}
	}

	/**
	 * 
	 * @Title: generateXS 
	 * @Description: 生成销售凭证方法抽离 
	 * @author heckerstone heckerstone@163.com
	 * @date Oct 29, 2020 10:04:53 AM 
	 * @param @param firstDay
	 * @param @param lastDay    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void generateXS(String firstDay, String lastDay) {
		//根据上述时间段查询所有销售出库
		QueryWrapper<StkIoBill> queryWrapper=new QueryWrapper<StkIoBill>();
		queryWrapper.ge("create_time", firstDay);
		queryWrapper.and(wrapper->wrapper.le("create_time", lastDay));
		queryWrapper.and(wrapper->wrapper.eq("stock_io_type", "201"));//查询销售出库单
		queryWrapper.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		queryWrapper.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		queryWrapper.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillList=stkIoBillService.list(queryWrapper);
		//非空验证--销售出库单
		if(null!=stkIoBillList&&stkIoBillList.size()>0) {
			for(int i=0;i<stkIoBillList.size();i++) {
				StkIoBill sib=stkIoBillList.get(i);
				BigDecimal total=BigDecimal.ZERO;
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询出库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//构造中间表数据--借
					Xiaoshouchuku zkcg=new Xiaoshouchuku();
					zkcg.setCreateBy("admin");
					zkcg.setCreateTime(sib.getCreateTime());
					zkcg.setBillNo(sib.getBillNo());
					zkcg.setBillType("销售出库单");
					zkcg.setBillMoney(total);
					zkcg.setAccountingType("主营业务成本");
					zkcg.setKindeeType("借");
					zkcg.setGroupId((i+1)+"");
					zkcg.setDepartId(departId);
					zkcg.setDepartName(departName);
					//构造中间表数据--贷
					Xiaoshouchuku zkcgD=new Xiaoshouchuku();
					zkcgD.setCreateBy("admin");
					zkcgD.setCreateTime(sib.getCreateTime());
					zkcgD.setBillNo(sib.getBillNo());
					zkcgD.setBillType("销售出库单");
					zkcgD.setBillMoney(total);
					zkcgD.setAccountingType("库存商品");
					zkcgD.setKindeeType("贷");
					zkcgD.setGroupId((i+1)+"");
					zkcgD.setDepartId(departId);
					zkcgD.setDepartName(departName);
					//保存数据
					xiaoshouchukuService.save(zkcg);
					xiaoshouchukuService.save(zkcgD);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		//销售退货入库
		QueryWrapper<StkIoBill> thckQw=new QueryWrapper<StkIoBill>();
		thckQw.ge("create_time", firstDay);
		thckQw.and(wrapper->wrapper.le("create_time", lastDay));
		thckQw.and(wrapper->wrapper.eq("stock_io_type", "2011"));//查询销售退货入库
		thckQw.and(wrapper->wrapper.eq("is_rubric", "1"));//退货标志
		thckQw.and(wrapper->wrapper.eq("is_approved", "1"));//是否通过
		thckQw.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		thckQw.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		List<StkIoBill> stkIoBillTHCKList=stkIoBillService.list(thckQw);
		if(null!=stkIoBillTHCKList&&stkIoBillTHCKList.size()>0) {
			//构造退货凭证
			for(int i=0;i<stkIoBillList.size();i++) {
				StkIoBill sib=stkIoBillList.get(i);
				BigDecimal total=BigDecimal.ZERO;
				//未生成过凭证
				if(sib.getIsUpload()==0) {
					//查询入库明细
					QueryWrapper<StkIoBillEntry> sibeQw=new QueryWrapper<StkIoBillEntry>();
					sibeQw.eq("mid",sib.getId());
					List<StkIoBillEntry> sibeList=stkIoBillEntryService.list(sibeQw);
					//计算凭证总和
					if(null!=sibeList&&sibeList.size()>0) {
						for(StkIoBillEntry si:sibeList) {
							total=total.add(si.getSettleAmt());//统计总金额
						}
					}
					String departId="";
					String departName="";
					QueryWrapper<SysDepart> sdQw=new QueryWrapper<SysDepart>();
					sdQw.eq("org_code", sib.getSysOrgCode());
					SysDepart sd=sysDepartService.getOne(sdQw.last("LIMIT 1"));
					if(null!=sd) {
						departId=sd.getId();
						departName=sd.getDepartName();
					}
					//构造中间表数据--借
					Xiaoshouchuku zkcg=new Xiaoshouchuku();
					zkcg.setCreateBy("admin");
					zkcg.setCreateTime(DateUtils.getDate());
					zkcg.setBillNo(sib.getBillNo());
					zkcg.setBillType("销售退货");
					zkcg.setBillMoney(total);
					zkcg.setAccountingType("库存商品");
					zkcg.setKindeeType("借");
					zkcg.setGroupId((i+1)+"");
					zkcg.setDepartId(departId);
					zkcg.setDepartName(departName);
					//构造中间表数据--贷
					Xiaoshouchuku zkcgD=new Xiaoshouchuku();
					zkcgD.setCreateBy("admin");
					zkcgD.setCreateTime(DateUtils.getDate());
					zkcgD.setBillNo(sib.getBillNo());
					zkcgD.setBillType("销售退货");
					zkcgD.setBillMoney(total);
					zkcgD.setAccountingType("主营业务成本");
					zkcgD.setKindeeType("贷");
					zkcgD.setGroupId((i+1)+"");
					zkcgD.setDepartId(departId);
					zkcgD.setDepartName(departName);
					//保存数据
					xiaoshouchukuService.save(zkcg);
					xiaoshouchukuService.save(zkcgD);
					sib.setIsUpload(1);
					stkIoBillService.updateById(sib);
				}
			}
		}
		//		//销售应收单--凭证
		//		QueryWrapper<FinReceivable> fpQw=new QueryWrapper<FinReceivable>();
		//		//		fpQw.eq("source_no", sib.getBillNo());
		//		fpQw.ge("create_time", firstDay);
		//		fpQw.and(wrapper->wrapper.le("create_time", lastDay));
		//		fpQw.and(wrapper->wrapper.eq("is_approved", "1"));//审核通过
		//		fpQw.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		//		fpQw.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		//		fpQw.and(wrapper->wrapper.eq("receivable_type", "102"));//应收单
		//		List<FinReceivable> fpableList=finReceivableService.list(fpQw);
		//		//非空
		//		if(null!=fpableList&&fpableList.size()>0) {
		//			for(int j=0;j<fpableList.size();j++) {
		//				FinReceivable fp=fpableList.get(j);
		//				//查询客户信息
		//				BasCustomer bs=basCustomerService.getById(fp.getCustomerId());
		//				String sId="";
		//				String sName="";
		//				try {
		//					sId=bs.getId();
		//					sName=bs.getName();
		//				} catch (Exception e) {
		//				}
		//				//构造中间表数据--借
		//				ZrkKindeeProofXsck zkcg2=new ZrkKindeeProofXsck();
		//				zkcg2.setCreateBy("admin");
		//				zkcg2.setCreateTime(fp.getCreateTime());
		//				zkcg2.setBillNo(fp.getBillNo());
		//				zkcg2.setBillType("销售应收单");
		//				zkcg2.setBillMoney(fp.getAmt());
		//				zkcg2.setAccountingType("银行存款-"+bankname+bankCardNo);
		//				zkcg2.setKindeeType("借");
		//				zkcg2.setGroupId((j+1)+"");
		//				zkcg2.setCustomerId(sId);
		//				zkcg2.setCustomerName(sName);
		//				//构造中间表数据--贷
		//				ZrkKindeeProofXsck zkcg3=new ZrkKindeeProofXsck();
		//				zkcg3.setCreateBy("admin");
		//				zkcg3.setCreateTime(fp.getCreateTime());
		//				zkcg3.setBillNo(fp.getBillNo());
		//				zkcg3.setBillType("销售应收单");
		//				zkcg3.setBillMoney(fp.getAmt());
		//				zkcg3.setAccountingType("应收账款");
		//				zkcg3.setKindeeType("贷");
		//				zkcg3.setGroupId((j+1)+"");
		//				zkcg3.setCustomerId(sId);
		//				zkcg3.setCustomerName(sName);
		//				//保存数据
		//				zrkKindeeProofXsckService.save(zkcg2);
		//				zrkKindeeProofXsckService.save(zkcg3);
		//			}
		//		}
		//		//销售预收单--凭证
		//		QueryWrapper<FinReceipt> fpQw2=new QueryWrapper<FinReceipt>();
		//		//				fpQw.eq("source_no", sib.getBillNo());
		//		fpQw2.ge("create_time", firstDay);
		//		fpQw2.and(wrapper->wrapper.le("create_time", lastDay));
		//		fpQw2.and(wrapper->wrapper.eq("is_approved", "1"));
		//		fpQw2.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		//		fpQw2.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		//		fpQw2.and(wrapper->wrapper.eq("receipt_type", "101"));//销售预收类型
		//		List<FinReceipt> fpmentList=finReceiptService.list(fpQw2);
		//		//非空
		//		if(null!=fpmentList&&fpmentList.size()>0) {
		//			for(int j=0;j<fpmentList.size();j++) {
		//				FinReceipt fp=fpmentList.get(j);
		//				//查询客户信息
		//				BasCustomer bs=basCustomerService.getById(fp.getCustomerId());
		//				String sId="";
		//				String sName="";
		//				try {
		//					sId=bs.getId();
		//					sName=bs.getName();
		//				} catch (Exception e) {
		//				}
		//				//构造中间表数据--借
		//				ZrkKindeeProofXsck zkcg2=new ZrkKindeeProofXsck();
		//				zkcg2.setCreateBy("admin");
		//				zkcg2.setCreateTime(fp.getCreateTime());
		//				zkcg2.setBillNo(fp.getBillNo());
		//				zkcg2.setBillType("销售预收款单");
		//				zkcg2.setBillMoney(fp.getAmt());
		//				zkcg2.setAccountingType("银行存款-"+bankname+bankCardNo);
		//				zkcg2.setKindeeType("借");
		//				zkcg2.setGroupId((j+1)+"");
		//				zkcg2.setCustomerId(sId);
		//				zkcg2.setCustomerName(sName);
		//				//构造中间表数据--贷
		//				ZrkKindeeProofXsck zkcg3=new ZrkKindeeProofXsck();
		//				zkcg3.setCreateBy("admin");
		//				zkcg3.setCreateTime(fp.getCreateTime());
		//				zkcg3.setBillNo(fp.getBillNo());
		//				zkcg3.setBillType("销售预收款单");
		//				zkcg3.setBillMoney(fp.getAmt());
		//				zkcg3.setAccountingType("应收帐款");
		//				zkcg3.setKindeeType("贷");
		//				zkcg3.setGroupId((j+1)+"");
		//				zkcg3.setCustomerId(sId);
		//				zkcg3.setCustomerName(sName);
		//				//保存数据
		//				zrkKindeeProofXsckService.save(zkcg2);
		//				zrkKindeeProofXsckService.save(zkcg3);
		//			}
		//		}
		//		//销售收款单--凭证
		//		QueryWrapper<FinReceipt> fpQw3=new QueryWrapper<FinReceipt>();
		//		//				fpQw.eq("source_no", sib.getBillNo());
		//		fpQw3.ge("create_time", firstDay);
		//		fpQw3.and(wrapper->wrapper.le("create_time", lastDay));
		//		fpQw3.and(wrapper->wrapper.eq("is_approved", "1"));
		//		fpQw3.and(wrapper->wrapper.eq("is_closed", "0"));//是否关闭
		//		fpQw3.and(wrapper->wrapper.eq("is_voided", "0"));//是否作废
		//		fpQw3.and(wrapper->wrapper.eq("receipt_type", "102"));//销售收款款类型
		//		List<FinReceipt> fpment2List=finReceiptService.list(fpQw3);
		//		//非空
		//		if(null!=fpment2List&&fpment2List.size()>0) {
		//			for(int j=0;j<fpment2List.size();j++) {
		//				FinReceipt fp=fpment2List.get(j);
		//				//查询客户信息
		//				BasCustomer bs=basCustomerService.getById(fp.getCustomerId());
		//				String sId="";
		//				String sName="";
		//				try {
		//					sId=bs.getId();
		//					sName=bs.getName();
		//				} catch (Exception e) {
		//				}
		//				//构造中间表数据--借
		//				ZrkKindeeProofXsck zkcg2=new ZrkKindeeProofXsck();
		//				zkcg2.setCreateBy("admin");
		//				zkcg2.setCreateTime(fp.getCreateTime());
		//				zkcg2.setBillNo(fp.getBillNo());
		//				zkcg2.setBillType("销售收款单");
		//				zkcg2.setBillMoney(fp.getAmt());
		//				zkcg2.setAccountingType("银行存款-"+bankname+bankCardNo);
		//				zkcg2.setKindeeType("借");
		//				zkcg2.setGroupId((j+1)+"");
		//				zkcg2.setCustomerId(sId);
		//				zkcg2.setCustomerName(sName);
		//				//构造中间表数据--贷
		//				ZrkKindeeProofXsck zkcg3=new ZrkKindeeProofXsck();
		//				zkcg3.setCreateBy("admin");
		//				zkcg3.setCreateTime(fp.getCreateTime());
		//				zkcg3.setBillNo(fp.getBillNo());
		//				zkcg3.setBillType("销售收款单");
		//				zkcg3.setBillMoney(fp.getAmt());
		//				zkcg3.setAccountingType("应收账款");
		//				zkcg3.setKindeeType("贷");
		//				zkcg3.setGroupId((j+1)+"");
		//				zkcg3.setCustomerId(sId);
		//				zkcg3.setCustomerName(sName);
		//				//保存数据
		//				zrkKindeeProofXsckService.save(zkcg2);
		//				zrkKindeeProofXsckService.save(zkcg3);
		//			}
		//		}
	}

}
